<?php

namespace App\Console\Commands;

use App\Mail\NoticeStudentResult;
use App\Models\Constant\StudentConstant;
use App\Models\Student\Point;
use App\Models\Student\Student;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Kkokk\Poster\PosterManager;

/**
 * 发送复试结果
 * Class SendNoticeStudentResult
 * @package App\Console\Commands
 */
class SendNoticeStudentResult extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'mail:noticeStudentResult';


    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle() {
        Log::info('mail:noticeStudentResult');
        $students = Point::where('is_send',2)->get();
        //dd($students);
        //$students = Point::whereIn('id',[19])->get();
        foreach ($students as $student) {
            Log::info('student'.$student->id);
            $title = "慧科国际复试结果通知";
            $studentId = $student->phone;
            $name = $student->name;
            $college = $student->college;
            $major = $student->major;
            $scholarship = $student->scholarship;
            $budgetFee = $student->budget_fee;
            $reviewPoint = $student->review_point;
            $interviewPoint = $student->interview_point;
            $gpa = $student->gpa;
            $imgPath = $this->generatePic($studentId,$name, $college, $major, $scholarship, $budgetFee, $reviewPoint, $interviewPoint, $gpa);
            Log::info('mail:noticeStudentResult',compact('imgPath'));
            $email = $student->email;
            if(!empty($email)){
                Mail::to($email)->send(new NoticeStudentResult($title, $name, $college,$major, $scholarship, $budgetFee, $reviewPoint, $interviewPoint, $gpa,$imgPath));
                $student->is_send = 1;
                $student->save();

                //修改学生状态
                if(!empty($student->phone)){
                    Student::where('phone',$student->phone)->update(['status'=>StudentConstant::STATUS_REINTERVIEW]);
                }


                Log::info('finished send');
            }
        }
        return 0;
    }

    public function generatePic($studentId,$name,$college,$major,$scholarship,$budgetFee,$reviewPoint, $interviewPoint, $gpa) {
        $src = public_path('/mail/bg.jpg');
        $content0 = $name;
        $content1 = $college.$major;
        $content2 = $scholarship.'美元';
        $content3 = $budgetFee.'美元';
        $content4 = $reviewPoint;
        $content5 = $gpa;
        $content6 = $interviewPoint;


        $nameLen = strlen($name);
        //三个字的名字
        if($nameLen<=6){
            $nameX = 115;
        }
        //两个字的名字
        elseif($nameLen>6 && $nameLen<=9){
            $nameX=85;
        }else{
            $nameX=65;
        }

        $buildTextManyArr = [
            ['content'=> $content0, 'dst_x' => $nameX, 'dst_y' => 247, 'rgba' => [51, 51, 51, 1], ],
            ['content'=> $content1, 'dst_x' => 489, 'dst_y' => 293, 'rgba' => [21, 143, 235, 1], ],
            ['content'=> $content2, 'dst_x' => 1128, 'dst_y' => 293, 'rgba' => [21, 143, 235, 1], ],
            ['content'=> $content3, 'dst_x' => 226, 'dst_y' => 335, 'rgba' => [21, 143, 235, 1], ],
            ['content'=> $content4, 'dst_x' => 398, 'dst_y' => 515, 'rgba' => [51, 51, 51, 1], ],
            ['content'=> $content5, 'dst_x' => 673, 'dst_y' => 515, 'rgba' => [51, 51, 51, 1], ],
            ['content'=> $content6, 'dst_x' => 906, 'dst_y' => 515, 'rgba' => [51, 51, 51, 1], ],
        ];

        foreach ($buildTextManyArr as &$item) {
            $item['weight'] = 3;
            $item['font_family'] = '';
            $item['max_w'] = 0;
            $item['font'] = 26;
        }
        $imgPath = '/mail/cert/'.$studentId.'_'.uniqid().'.png';
        $result = PosterManager::Poster()
            ->path(public_path($imgPath))
            ->buildImDst($src)
            ->buildTextMany($buildTextManyArr)
            ->getPoster();

        Log::debug('sds',[$result]);

        return $imgPath;
    }
}
